package com.bit101.components {
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class TextArea extends Text {
		protected var _scrollbar:VScrollBar;

		public function TextArea(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, text:String = ""){
			super(parent, xpos, ypos, text);
		}

		/**
		 * 初始化，侦听鼠标滚轮事件
		 */
		protected override function init():void {
			super.init();
			addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}

		/**
		 * 添加竖型滚动条
		 * 文本侦听滚动事件
		 */
		override protected function addChildren():void {
			super.addChildren();
			_scrollbar = new VScrollBar(this, 0, 0, onScrollbarScroll);
			_tf.addEventListener(Event.SCROLL, onTextScroll);
		}

		/**
		 * Changes the thumb percent of the scrollbar based on how much text is shown in the text area.
		 */
		protected function updateScrollbar():void {
			var visibleLines:int = _tf.numLines - _tf.maxScrollV + 1;
			var percent:Number   = visibleLines / _tf.numLines;
			_scrollbar.setSliderParams(1, _tf.maxScrollV, _tf.scrollV);
			_scrollbar.setThumbPercent(percent);
			_scrollbar.pageSize = visibleLines;
		}

		/**
		 * 绘制UI
		 */
		override public function draw():void {
			super.draw();
			_tf.width    = _width - _scrollbar.width - 4;
			_scrollbar.x = _width - _scrollbar.width;
			_scrollbar.height = _height;
			_scrollbar.draw();
			addEventListener(Event.ENTER_FRAME, onTextScrollDelay);
		}

		///////////////////////////////////
		// event handlers
		///////////////////////////////////
		/**
		 * Waits one more frame before updating scroll bar.
		 * It seems that numLines and maxScrollV are not valid immediately after changing a TextField's size.
		 */
		protected function onTextScrollDelay(event:Event):void {
			removeEventListener(Event.ENTER_FRAME, onTextScrollDelay);
			updateScrollbar();
		}

		/**
		 * Called when the text in the text field is manually changed.
		 */
		protected override function onChange(event:Event):void {
			super.onChange(event);
			updateScrollbar();
		}

		/**
		 * 文本字段中的垂直位置
		 */
		protected function onScrollbarScroll(event:Event):void {
			_tf.scrollV = Math.round(_scrollbar.value);
		}

		/**
		 * 文本滚动事件，滚动值
		 */
		protected function onTextScroll(event:Event):void {
			_scrollbar.value = _tf.scrollV;
			updateScrollbar();
		}

		/**
		 * 滚轮滚动值
		 */
		protected function onMouseWheel(event:MouseEvent):void {
			event.stopImmediatePropagation();
			
			_scrollbar.value -= event.delta;
			_tf.scrollV = Math.round(_scrollbar.value);
		}

		/**
		 * Sets/gets whether this component is enabled or not.
		 */
		public override function set enabled(value:Boolean):void {
			super.enabled = value;
			_tf.tabEnabled = value;
		}

		/**
		 * Sets / gets whether the scrollbar will auto hide when there is nothing to scroll.
		 */
		public function set autoHideScrollBar(value:Boolean):void {
			_scrollbar.autoHide = value;
		}

		public function get autoHideScrollBar():Boolean {
			return _scrollbar.autoHide;
		}
	}
}